
<template>
  <div class="table-page">
    <div class="query">
      <el-form label-position="left" :inline='true'>
        <el-row>
          <el-col>
            <el-button type="success" v-on:click="add" style="margin-top:20px;margin-left:20px">新增</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="app-container">
      <el-table :data="this.list" border  v-loading="loading">
        
        <el-table-column type="index" label="行号" width="50" header-align="center" align="center"></el-table-column>
        <el-table-column prop="account" label="账号"></el-table-column>      
        <el-table-column  prop="name" label="用户名"></el-table-column>
        <el-table-column
      prop="roleid"
      label="角色"
      :formatter="GetRoleName">
    </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click.native.prevent="edit(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click.native.prevent="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="新增用户" :visible.sync="openEditWindow" :close-on-click-modal="false">
      <userEdit ref="userEdit" @refresh="refresh"></userEdit>
    </el-dialog>
  </div>
</template>

<script>
import userEdit from "./edit";
import {getList,remove} from "@/api/user"

export default {
    name: "index",
  data() {
    return {
      keyword: "",
      loading: false,
      openEditWindow: false,
      tableHeight: window.innerHeight - 260,
      list: null,
      role:["","管理员","组长","组员"]
    };
  },
  components: {
    userEdit
  },
  created() {
    this.query();
  },
  methods: {
    query() {
      getList().then(Response=>{
        this.list=Response.data
        this.loading=false
      })
    },
    add() {
      this.openEditWindow = true;
      //需要子组件渲染DOM后才能执行
      this.$nextTick(() => {
        this.$refs.userEdit.initData();
      });
    },
    edit(m) {
      this.openEditWindow = true;
      //需要子组件渲染DOM后才能执行
      this.$nextTick(() => {
        this.$refs.userEdit.initData(m);
      });
    },
    refresh() {
      this.openEditWindow = false;
      this.query();
    },
    filterTag(value, row){
      return row.tag === value;
    },

    del(m){
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          remove(m.id).then(Response=>{
            if (Response.success){
              this.$message({
              type: 'success',
              message: '删除成功!'
              });
              this.refresh()
            }else{
              this.$message({
              type: 'warning',
              message: '删除失败!'
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    GetRoleName(row, column){
      return this.role[row.roleid]
    }
  }
};
</script>

